(function () {
     
    var _current_dialog = null;
    var _prevent_close = false;
    var _tpl_container = document.getElementById("tpl_container");
    var _dom_editor_box = document.getElementById("dom_editor_box");
    var _tool_bar_box = document.getElementById('tool_bar');

    var showInToolBox = function (dialog) {
        if (_current_dialog) {
            _prevent_close = true;
            _current_dialog.close();
        }
        _current_dialog = dialog;
         
        _tpl_container.setAttribute("width", "70%");
        _tpl_container.style.width = '70%'; 
        _dom_editor_box.style.width = '30%';
        window.frames[0].document.body.style.scale = 0.7;
        window.frames[0].document.body.style.transformOrigin = 'top';
        dialog.showDialog(_dom_editor_box).load();
         
    }

    var closeToolBox = function () {
        if (_prevent_close) {
            _prevent_close = false;
            return;
        }
        _tpl_container.setAttribute("width", "100%");
        _tpl_container.style.width = '100%';
        _dom_editor_box.style.width = 0; 
        window.frames[0].document.body.style.scale = 1;
        return
    }
    var afterShowLayouter = function (layouter)
    { 
        _tpl_container.style.transformDuration = '1s';
        _tpl_container.style.left = '-100%';

        _tool_bar_box.style.transformDuration = '1s';
        _tool_bar_box.style.position = 'absolute';
        _tool_bar_box.style.top = '-100px';
        //注册关闭后事件
        layouter.on('afterClose', function(){ 
            _tpl_container.style.left = '0'; 
            _tool_bar_box.style.position = null;
            _tool_bar_box.style.top = '0';
        });
        
    };
    var previewPage = function(page,data)
    {
        console.log(page,data);
        var key= document.getElementById('pageKey').value;
        //获取页面数据和模板数据
        Rsd.popup('preview page key:' + key);

    }
    //入口 网站模板数据源编辑容器，可以编辑 数据 ，不能编辑布局
    window.main = function () {

        var app = this;
        app.appTitle = "Page Editor";
        app.appName = "HTML编辑神器";
        app.checkSessionServiceName = document.getElementById("checkSessionService").value;
        app.loginServiceName = "";
        var _getPageApp=function(){return window.frames[0].window.Rsd.app;};
         
        //工具条
        setTimeout(function () {
             
            var key= document.getElementById('pageKey').value;
            
            var bar = Rsd.create('Rsd.tool.PageEditBar', {
                header: false,
                modular:false,
                height: '100%',
                key:key,
                parent: document.getElementById('tool_bar'),
                documentIframe: 'tpl_container',
                documentTitle: document.getElementById('pageTitle').value,
                editMode:document.getElementById('editMode').value,
                domSelector: document.getElementById('domSelector').value,
                getPageApp:_getPageApp,
                onShowDomEditor: showInToolBox,
                onBeforeCloseEditor: closeToolBox,
                onAfterShowLayouter:afterShowLayouter,
                saveHandler:'',
                previewHandler:previewPage,
                publishHandler:''
            }).show();
            bar.load();
          
        }, 1000);

    }
     
})()